package com.huawei.eihealth.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ServerResponseException;
import com.huaweicloud.sdk.eihealth.v1.EiHealthClient;
import com.huaweicloud.sdk.eihealth.v1.model.AppDto;
import com.huaweicloud.sdk.eihealth.v1.model.AppInputParameterDto;
import com.huaweicloud.sdk.eihealth.v1.model.CreateAppRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CreateAppDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(CreateAppDemo.class);

    public static void main(String[] args) {

        // 设置AK和SK
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        // projectId 云服务所在项目 ID
        String projectId = "<YOUR PROJECT_ID>";

        // endpoint 华为云各服务应用区域和各服务的终端节点
        String endpoint = "<YOUR ENDPOINT>";

        // 初始化客户端
        BasicCredentials auth = new BasicCredentials().withAk(ak).withSk(sk).withProjectId(projectId);
        EiHealthClient eiHealthClient = EiHealthClient.newBuilder().withCredential(auth).withEndpoint(endpoint).build();

        CreateAppRequest createAppRequest = new CreateAppRequest();

        // 设置医疗智能体的项目id
        createAppRequest.setEihealthProjectId("your eihealth_project_id");

        // 初始化APP请求体
        AppDto appDto = initAppDto();
        createAppRequest.setBody(appDto);

        try {
            eiHealthClient.createApp(createAppRequest);
        } catch (ClientRequestException | ServerResponseException e) {
            LOGGER.error(String.valueOf(e.getHttpStatusCode()));
            LOGGER.error(e.getErrorCode());
            LOGGER.error(e.getMessage());
        }
    }

    private static AppDto initAppDto() {
        AppDto appDto = new AppDto();
        appDto.setName("demo-name");
        appDto.setVersion("1.0");
        appDto.setCommands(Collections.singletonList("demo commands"));
        appDto.setImage("demo-image");
        List<AppInputParameterDto> inputs = new ArrayList<>();
        AppInputParameterDto appInputParameterDto = new AppInputParameterDto();
        appInputParameterDto.setName("input");
        appInputParameterDto.setType("FILE");
        appInputParameterDto.setRequired(false);
        inputs.add(appInputParameterDto);

        appDto.setInputs(inputs);
        appDto.setOutputs(new ArrayList<>());
        return appDto;
    }
}
